package com.example.androidsx;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    private final ArrayList<Object> productList;
    private final Context context;
    private Object product;
    private final OnCartUpdateListener cartUpdateListener;

    public interface OnCartUpdateListener {
        void onCartUpdated(Object product);

        void onCartUpdated(com.google.android.gms.analytics.ecommerce.Product product);

        void onCartUpdated(Product product);
    }

    public ProductAdapter(ArrayList<Object> productList, Context context, OnCartUpdateListener listener) {
        this.productList = productList;
        this.context = context;
        this.product = product;
        this.cartUpdateListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_product, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Product product = (Product) productList.get(position);
        holder.productName.setText(product.getName());
        holder.productPrice.setText(product.getPrice());
        holder.productDescription.setText(product.getDescription());

        // 加载商品图片
        int resId = context.getResources().getIdentifier(
                product.getImageName(),
                "drawable",
                context.getPackageName()
        );
        if (resId != 0) {
            holder.productImage.setImageResource(resId);
        } else {
            holder.productImage.setImageResource(R.drawable.ic_default);
        }

        // 设置数量
        holder.tvQuantity.setText(String.valueOf(product.getQuantity()));

        // 减少按钮点击
        holder.btnDecrease.setOnClickListener(v -> {
            int quantity = product.getQuantity();
            if (quantity > 0) {
                product.setQuantity(quantity - 1);
                holder.tvQuantity.setText(String.valueOf(product.getQuantity()));
                notifyItemChanged(position);
                if (cartUpdateListener != null) {
                    cartUpdateListener.onCartUpdated(product);
                }
            }
        });

        // 增加按钮点击
        holder.btnIncrease.setOnClickListener(v -> {
            int quantity = product.getQuantity();
            product.setQuantity(quantity + 1);
            holder.tvQuantity.setText(String.valueOf(product.getQuantity()));
            notifyItemChanged(position);
            if (cartUpdateListener != null) {
                cartUpdateListener.onCartUpdated(product);
            }
        });

        // 按钮点击动画
        setButtonTouchEffect(holder.btnDecrease);
        setButtonTouchEffect(holder.btnIncrease);

        // 商品项点击事件
        holder.cardView.setOnClickListener(v -> {
            Toast.makeText(context, product.getName() + " 已选择", Toast.LENGTH_SHORT).show();
        });
    }

    private void setButtonTouchEffect(Button button) {
        button.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    v.animate().scaleX(0.9f).scaleY(0.9f).setDuration(100).start();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    v.animate().scaleX(1f).scaleY(1f).setDuration(100).start();
                    break;
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView productName, productPrice, productDescription, tvQuantity;
        ImageView productImage;
        Button btnDecrease, btnIncrease;
        CardView cardView;

        public ViewHolder(View itemView) {
            super(itemView);
            productName = itemView.findViewById(R.id.product_name);
            productPrice = itemView.findViewById(R.id.product_price);
            productDescription = itemView.findViewById(R.id.product_description);
            productImage = itemView.findViewById(R.id.product_image);
            btnDecrease = itemView.findViewById(R.id.btn_decrease);
            btnIncrease = itemView.findViewById(R.id.btn_increase);
            tvQuantity = itemView.findViewById(R.id.tv_quantity);
            cardView = itemView.findViewById(R.id.card_view);
        }
    }
}